package com.js.nickcar.module.addcar.valuation;


import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.js.nickcar.R;
import com.js.nickcar.base.BaseFragment;
import com.js.nickcar.entity.remote.CarInfoBean;
import com.js.nickcar.impl.TextWatcherImpl;
import com.js.nickcar.utils.StringUtils;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by LZY on 2017/10/10.
 */

public class ValuationEntryFragment extends BaseFragment<ValuationEntryPresenter> implements ValuationEntryView {
    @Bind(R.id.tv_car_brand)
    TextView mTvCarBrand;
    @Bind(R.id.tv_info)
    TextView mTvInfo;
    @Bind(R.id.tv_purchasing_price)
    TextView mTvPurchasingPrice;
    @Bind(R.id.tv_selling_price)
    TextView mTvSellingPrice;
    @Bind(R.id.et_valuation_entry)
    EditText mEtValuationEntry;
    @Bind(R.id.et_additional_remarks)
    EditText mEtAdditionalRemarks;
    @Bind(R.id.btn_commit)
    Button BtnCommit;
    @Bind(R.id.tv_valuation_entry_unit)
    TextView mTvValuationEntryUnit;
    private static final String EXTRA_CAR_ID = "car_id";

    @Override
    public int getLayoutId() {
        return R.layout.fragment_valuation_entry;
    }

    @Override
    protected void initBundle(Bundle bundle) {
        super.initBundle(bundle);
        mPresenter.setCarId(bundle.getString(EXTRA_CAR_ID));
    }

    @Override
    protected ValuationEntryPresenter createPresenter() {
        return new ValuationEntryPresenter();
    }

    @Override
    protected String getTitle() {
        return getString(R.string.valuation_entry);
    }

    public static ValuationEntryFragment getInstance(String carId) {
        ValuationEntryFragment entryFragment = new ValuationEntryFragment();
        Bundle bundle = new Bundle();
        bundle.putString(EXTRA_CAR_ID, carId);
        entryFragment.setArguments(bundle);
        return entryFragment;
    }

    @Override
    protected void initData() {
        mEtValuationEntry.setHint(StringUtils.getSizeSpan(getString(R.string.please_input_valuation_entry), 15));
        mPresenter.getCarInfo();
    }

    @OnClick(R.id.btn_commit)
    public void onClick() {
        String valuationEntry = mEtValuationEntry.getText().toString().trim();
        if (TextUtils.isEmpty(valuationEntry)) {
            showToast(getString(R.string.please_input_you_valuation_entry));
            return;
        }
        String additionalRemarks = mEtAdditionalRemarks.getText().toString().trim();
        mPresenter.commitCarPrice(valuationEntry, additionalRemarks);
    }

    @Override
    public void getCarInfoSuccess(CarInfoBean carInfoBean) {
        mTvCarBrand.setText(carInfoBean.getCar_type());
        mTvInfo.setText(carInfoBean.getCar_condition());
        mTvPurchasingPrice.setText(getString(R.string.the_car_dealer_price) + carInfoBean.getReference_price() + getString(R.string.ten_thousand));
        mTvSellingPrice.setText(getString(R.string.the_car_dealer_seller) + carInfoBean.getSeller_price() + getString(R.string.ten_thousand));
    }

    @Override
    public void onCommitCarPriceSuccess() {
        getTargetFragment().onActivityResult(getTargetRequestCode(), Activity.RESULT_OK, null);
        finish();
    }

    @Override
    protected void setListener() {
        super.setListener();
        mEtValuationEntry.addTextChangedListener(new TextWatcherImpl() {
            @Override
            public void afterTextChanged(Editable s) {
                super.afterTextChanged(s);
                mTvValuationEntryUnit.setVisibility(TextUtils.isEmpty(mEtValuationEntry.getText()) ? View.GONE : View.VISIBLE);
            }
        });
    }
}
